<?php

App::uses('CrudController', 'Controller');
App::uses('Areas', 'Model');

class InscriptionsController extends CrudController
{
    public $_model = 'Inscription';

    public function admin($areaId = null) {
        $this->request->allowMethod('get');
        $this->loadModel('Area');
        $areas = $this->Area->getEnabled();
        $this->set('areas', $areas);
        $this->set('areaId', $areaId);
        $this->set('title', __('Inscripción a cursos'));
        $this->set('description', __('Administración'));
    }

    /**
     * Registramos una inscripcion para un usuario y un examen
     */
    public function register($scheduleId) {
        $this->request->allowMethod('get');
        $result = false;
        $userId = $this->Auth->user('id');
        $inscription = array(
            'schedule_id' => $scheduleId,
            'user_id' => $userId,
        );
        $this->Inscription->save($inscription);
        $result = true;
        $this->set(array('success' => $result, '_serialize' => 'success'));
        $this->viewClass = 'Json';
    }

    public function unregister($id) {
        $this->request->allowMethod('get');
        $this->Inscription->destroy($id, Inscription::STATUS_DISABLED);
        $this->set(array('success' => true, '_serialize' => 'success'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabledByArea($param);
        return $records;
    }

    protected function c($model)
    {
        return array(
            'schedule_id' => $model->schedule_id,
            'user_id' => $model->user_id,
        );
    }

    protected function u($model)
    {
        return array(
            'area_id' => $model->area_id,
            'name' => $model->name,
        );
    }

}
